package bpmlab.kivulifx.controller;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import bpmlab.kivulifx.application.Context;
import bpmlab.kivulifx.entity.Project;
import bpmlab.kivulifx.persistence.Persistence;
import bpmlab.kivulifx.persistence.PersistenceFactory;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 * @author bpmlab
 */
public class NewProjectController implements Initializable {

    @FXML
    private TextField projectName;
    @FXML
    private TextArea description;
    @FXML
    private TextField filePath;
    @FXML
    private Button btnCreate;
    @FXML
    private Label msgNameProject;
    @FXML
    private Label msgFilePath;

    private boolean okClicked = false;
    private Stage dialog;
    private Project project;
    private File file;

    /**
     * Initializes the controller class.
     *
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        validator();
        String workspace = Context.getCurrentInstance().getWorkspace();
        filePath.setText(workspace);
        projectName.setOnKeyReleased((KeyEvent event) -> {
            filePath.setText(workspace + File.separator + projectName.getText() + ".kv");
        });
    }

    private void validator() {
        btnCreate.disableProperty().bind(projectName.textProperty().isEmpty());
        msgNameProject.visibleProperty().bind(projectName.textProperty().isEmpty());
        msgFilePath.visibleProperty().bind(filePath.textProperty().isEmpty());
    }

    @FXML
    public void create() throws Exception {
        String name = projectName.getText();
        String path = filePath.getText();
        String desc = description.getText();
        project = new Project(name, desc, path);

        Context context = Context.getCurrentInstance();
        context.projects().put(name, project);
        context.setCurrentProject(project);

        Persistence persistence = PersistenceFactory.createPersistence();
        persistence.save(project);
        okClicked = true;
        dialog.close();
    }

    @FXML
    public void search() {
        DirectoryChooser chooser = new DirectoryChooser();
        file = chooser.showDialog(null);
        if (file != null) {
            filePath.setText(file.getPath() + File.separator);
            if (!projectName.getText().isEmpty()) {
                filePath.setText(file.getPath() + File.separator + projectName.getText() + ".kv");
            }
        }
    }

    @FXML
    public void close() {
        dialog.close();
    }

    public boolean isOkClicked() {
        return okClicked;
    }

    public void setDialog(Stage dialog) {
        this.dialog = dialog;
    }

}
